<template>
  <div v-loading="loading" class="dashboard-container">
    <el-card class="tree-card">
      <TreeTools :tree-node="company" :is-root="false" @addDepts="addDepts" />
      <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
        <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
        <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
        <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
        <TreeTools slot-scope="{data}" :tree-node="data" @editDepts="editDepts" @delDepts="delDepts" @addDepts="addDepts" />
      </el-tree></el-card>
    <AddDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @delDepts="getDepartments" />
  </div>
</template>

<script>
import AddDept from './components/add-dept.vue'
import { tranListToTreeData } from '@/utils'
import { getDepartments } from '@/api/departments'
import TreeTools from './components/tree-tools.vue'
export default {
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      loading: false, // 页面加载
      company: {},
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const res = await getDepartments()
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      this.departs = tranListToTreeData(res.depts, '')
      this.loading = false
    },
    delDepts() {
      this.getDepartments()
    },
    // 显示添加对话框
    addDepts(node) {
      this.showDialog = true
      // 存储添加当前项节点
      this.node = node
    },
    // 显示编辑对话框回显
    editDepts(node) {
      this.showDialog = true
      this.node = node
      // 通过refs 调用子组件的方法并传递当前部门的id
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
